#include "TK.h"
int flag = 1;

void Q_init(struct Queue* queue){
	queue->front = NULL;
	queue->rear = NULL;
	queue->size = 0;
}

bool enqueue(struct Queue* queue , char val , int max){
	elem* new;
	if(flag)
	{
		new = (elem*)malloc(sizeof(elem));
		new->next = NULL;
	}
	else
	new = queue->rear->next;

	if (queue->size == max)
	{
		flag = 0;
		queue->rear->next = queue->front;
		return false;
	}

	new->data = val;

	if(queue->size == 0)
	{
		queue->front = new;
		queue->rear = new;
	}
	else
	{
			queue->rear->next = new;
			queue->rear = new;
	}
	queue->size++;
	return true;
}

bool dequeue(struct Queue* queue , char* data){
	if(queue->size == 0)
	return false;
	else
	{
		if(flag)
		{
			elem* tmp = queue->front;
			*data = queue->front->data;
			queue->front = queue->front->next;
			free(tmp);
		}
		else
		{
			*data = queue->front->data;
			queue->front = queue->front->next;
		}
		queue->size--;
		return true;
	}
}

void destroy(struct Queue* queue){
	elem* head;
	if(flag)
	head = queue->front;
	else
	{
		head = queue->front->next;
		queue->front->next = NULL;
	}
	while(head)
	{
		elem* tmp = head;
		head = head->next;
		free(tmp);
	}
	queue->size = 0;
}
